#include "user_module.h"

#include "protocol_client.h"
#include "repo.h"
#include "weilapb.h"
#include "weilapb_defines.h"
#include "wlsdk_define.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
    wlsdk_message_callback callback;
} wl_user_global;

static wl_user_global s_user_global;

static wl_int32 user_protocol_client_listen_callback(PBWeilaMsg* message, void* context);
static wl_int32 user_message_callback(wl_int32 msgid, void* data, wl_uint32 datalen);
static void     process_user_changed_message(PBWeilaMsg* message, void* context);

static void user_sdk_message_callback(wl_uint32 msgid, wl_int32 resultcode, void* msgdata, wl_uint32 datalen) {
    if (s_user_global.callback) {
        s_user_global.callback(msgid, resultcode, msgdata, datalen);
    }
}

wl_int32 user_module_init(wlsdk_message_callback callback) {
    wl_int32  ret = 0;
    wl_module module;

    WLLOGD("user_module_init");
    memset(&module, 0, sizeof(wl_module));
    module.moduleid = WL_MODULE_USER;
    module.taskid   = WL_TASK_APP;
    module.callback = user_message_callback;
    ret             = wl_core_register_module(&module);
    if (0 != ret) {
        WLLOGW("wl_core_register_module user fail");
        return ret;
    }
    s_user_global.callback = callback;

    protocol_client_add_listen(
        WL_USER_CHANGE_USER_INFO_MSG, WEILA_MSG_IND, user_protocol_client_listen_callback, wl_null);
    protocol_client_add_listen(WL_USER_SET_CONFIG_MSG, WEILA_MSG_IND, user_protocol_client_listen_callback, wl_null);
    return ret;
}
wl_int32 user_module_end(void) {
    WLLOGD("user_module_end");
    wl_core_unregister_module(WL_MODULE_USER);
    s_user_global.callback = wl_null;
    return 0;
}

static wl_int32 user_message_callback(wl_int32 msgid, void* data, wl_uint32 datalen) {
    WLLOGD("msgid %d", msgid);
    switch (msgid) {
        case USER_MSG_GET_SELFINFO: {
            repo_user userinfo;
            repo_get_selfinfo(&userinfo);
            user_sdk_message_callback(
                WLSDK_MSG_USER_SELFINFO, WLSDK_RESULT_SUCCESS, &userinfo.user, sizeof(wlsdk_user));
            break;
        }
        case USER_MSG_GET_USER_CONFIG: {
            wl_char* config = repo_get_user_config();
            if (config) {
                user_sdk_message_callback(WLSDK_MSG_USER_CONFIG, WLSDK_RESULT_SUCCESS, config, strlen(config) + 1);
                // repo_set_user_config(wl_null);
            } else {
                user_sdk_message_callback(WLSDK_MSG_USER_CONFIG, WLSDK_RESULT_SUCCESS, wl_null, 0);
                // repo_set_user_config(
                //     "{\"ptt_button\":[{\"k1\":[{\"did\":\"0\",\"func\":\"01\",\"to\":\"01\"}],\"k2\":[{\"did\":\"0\","
                //     "\"func\":\"01\",\"to\":\"01\"}],\"k3\":[{\"did\":\"20118050\",\"func\":\"01\",\"to\":\"01\"}],"
                //     "\"k4\":[{\"did\":\"20118050\",\"func\":\"01\",\"to\":\"01\"}],\"k5\":[{\"did\":\"0\",\"func\":"
                //     "\"06\",\"to\":\"01\"}],\"t\":\"02\",\"version\":0}],\"version\":11}");
            }
            break;
        }
        default:
            WLLOGW("msgid %d not support", msgid);
            break;
    }
    return 0;
}

static wl_int32 user_protocol_client_listen_callback(PBWeilaMsg* message, void* context) {
    WLLOGD("message %d", message->message);
    switch (message->message) {
        case WL_USER_CHANGE_USER_INFO_MSG:
            process_user_changed_message(message, context);
            break;
        case WL_USER_SET_CONFIG_MSG: {
            PB_CHAR* config = (PB_CHAR*)message->pContent;
            repo_set_user_config(config);
            if (config) {
                user_sdk_message_callback(WLSDK_MSG_USER_CONFIG, WLSDK_RESULT_SUCCESS, config, strlen(config) + 1);
            } else {
                user_sdk_message_callback(WLSDK_MSG_USER_CONFIG, WLSDK_RESULT_SUCCESS, wl_null, 0);
            }
            break;
        }
        default:
            break;
    }
    return 0;
}

void process_user_changed_message(PBWeilaMsg* message, void* context) {
    PBUserInfo* userInfo = (PBUserInfo*)message->pContent;
    repo_user   user_info;
    repo_user   self_info;
    repo_get_selfinfo(&self_info);
    memset(&user_info, 0, sizeof(repo_user));
    user_info.type        = self_info.type;  // type使用登录用户类型
    user_info.user.userid = userInfo->userId;
    strncpy(user_info.user.number, userInfo->number, MAX_ACCOUNT_LEN);
    user_info.user.number[MAX_ACCOUNT_LEN - 1] = '\0';
    user_info.user.sex                         = userInfo->sex;
    strncpy(user_info.user.nick, userInfo->nick, MAX_NAME_LEN);
    user_info.user.nick[MAX_NAME_LEN - 1] = '\0';
    strncpy(user_info.user.phone, userInfo->phone, MAX_ACCOUNT_LEN);
    user_info.user.phone[MAX_ACCOUNT_LEN - 1] = '\0';
    strncpy(user_info.user.country_code, userInfo->countryCode, MAX_COUNTRYCODE_LEN);
    user_info.user.country_code[MAX_COUNTRYCODE_LEN - 1] = '\0';
    WLLOGD("user id %d number %s sex %d nick %s phone %s",
           user_info.user.userid,
           user_info.user.number,
           user_info.user.sex,
           user_info.user.nick,
           user_info.user.phone);
    repo_set_selfinfo(&user_info);
    user_sdk_message_callback(WLSDK_MSG_USER_SELFINFO, WLSDK_RESULT_SUCCESS, &user_info.user, sizeof(wlsdk_user));
}

#ifdef __cplusplus
}
#endif
